// pages/photos/photos.js
const config = require('../../config.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        baseUrl: config.basePath,
        fullLoading: true,
        photosLoding: true,
        photosData: [],
        total: 0,
        pageIndex: 1,
        showPassword: false,
        password: '',
        clickId: '',
        clickName: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.setNavigationBarTitle({
            title: "相册"
        });
        this.getPhotosList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.setData({
            fullLoading: false
        });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        if (that.data.total <= that.data.photosData.length) {
            return;
        }
        that.setData({
            pageIndex: that.data.pageIndex + 1
        });
        that.getPhotosList();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    onShareTimeline(res) {
        return {
            title: config.miniName + "-相册",
            path: '/pages/photos/photos'
        }
    },
    getPhotosList: function () {
        let that = this;
        that.setData({
            photosLoding: true
        });
        wx.request({
            url: config.basePath + '/mini/photos/list',
            method: 'GET',
            data: {
                pageIndex: that.data.pageIndex,
                pageSize: 5,
                name: ''
            },
            success(res) {
                if (res.data.code === 200) {
                    that.setData({
                        photosData: that.data.photosData.concat(res.data.data),
                        total: res.data.total,
                        photosLoding: true
                    });

                    if (that.data.total <= that.data.photosData.length) {
                        that.setData({
                            photosLoding: false
                        });
                    }
                } else {
                    wx.lin.showMessage({
                        type: 'error',
                        content: '服务出错'
                    })
                }
            },
            fail(err) {
                that.setData({
                    photosLoding: false
                });
                wx.lin.showMessage({
                    type: 'error',
                    content: '网络出错'
                })
            }
        });
    },
    goPhoto: function (event) {
        let that = this;
        that.setData({
            clickId: event.currentTarget.dataset.id,
            clickName: event.currentTarget.dataset.name
        });
        if (event.currentTarget.dataset.isencryption == 1) {
            that.setData({
                showPassword: true
            });
            return;
        }
        wx.navigateTo({
            url: "/pages/photo/photo?id=" + event.currentTarget.dataset.id + '&name=' + event.currentTarget.dataset.name + '&password= '
        })
    },
    passwordConfirm: function (e) {
        let that = this;
        wx.request({
            url: config.basePath + '/mini/photos/verify',
            method: 'POST',
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
                id: that.data.clickId,
                password: that.data.password
            },
            success(res) {
                if (res.data.code == 200) {
                    wx.navigateTo({
                        url: "/pages/photo/photo?id=" + that.data.clickId + '&name=' + that.data.clickName + '&password=' + that.data.password
                    })
                } else {
                    wx.lin.showMessage({
                        type: 'error',
                        content: res.data.msg
                    });
                }
                that.setData({
                    password: ''
                });
            },
            fail(err) {
                that.setData({
                    password: ''
                });
                wx.lin.showMessage({
                    type: 'error',
                    content: '网络出错'
                })
            }
        });
    },
    passwordCancel: function (e) {
        that.setData({
            password: ''
        });
    },
    changePasswordInput: function (e) {
        let that = this;
        that.setData({
            password: e.detail.value
        });
    }
})